<template><div><h2 id="响应" tabindex="-1"><a class="header-anchor" href="#响应"><span>响应</span></a></h2>
<ul>
<li><a href="#creating-responses">创建响应</a>
<ul>
<li><a href="#attaching-headers-to-responses">添加响应头</a></li>
<li><a href="#attaching-cookies-to-responses">添加响应 Cookies</a></li>
<li><a href="#cookies-and-encryption">Cookies &amp; 加密</a></li>
</ul>
</li>
<li><a href="#redirects">重定向</a>
<ul>
<li><a href="#redirecting-named-routes">重定向到命名路由</a></li>
<li><a href="#redirecting-controller-actions">重定向到控制器方法</a></li>
<li><a href="#redirecting-external-domains">重定向到外部域名</a></li>
<li><a href="#redirecting-with-flashed-session-data">重定向并使用闪存的 Session 数据</a></li>
</ul>
</li>
<li><a href="#other-response-types">其它响应类型</a>
<ul>
<li><a href="#view-responses">视图响应</a></li>
<li><a href="#json-responses">JSON 响应</a></li>
<li><a href="#file-downloads">文件下载</a></li>
<li><a href="#file-responses">文件响应</a></li>
</ul>
</li>
<li><a href="#response-macros">响应宏</a></li>
</ul>
<h2 id="创建响应" tabindex="-1"><a class="header-anchor" href="#创建响应"><span>创建响应</span></a></h2>
<h4 id="字符串-数组" tabindex="-1"><a class="header-anchor" href="#字符串-数组"><span>字符串 &amp; 数组</span></a></h4>
<p>所有路由和控制器处理完业务逻辑之后都会返回响应到用户的浏览器，Laravel 提供了多种不同的响应方式，其中最基本就是从路由或控制器返回一个简单的字符串，框架会自动将这个字符串转化为一个完整的 HTTP 响应：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除了从路由和控制器返回字符串之外，你还可以返回数组。 框架会自动将数组转换为 JSON 响应：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
你知道从路由或控制器还可以返回 <a href="https://learnku.com/docs/laravel/10.x/eloquent-collections" target="_blank" rel="noopener noreferrer">Eloquent 集合</a>吗？他们也会自动转化为 JSON 响应！</p>
</blockquote>
<h4 id="response-对象" tabindex="-1"><a class="header-anchor" href="#response-对象"><span>Response 对象</span></a></h4>
<p>通常情况下会只返回简单的字符串或数组，大多数时候，需要返回一个完整的<code v-pre>Illuminate\Http\Response</code>实例或是<a href="https://learnku.com/docs/laravel/10.x/views" target="_blank" rel="noopener noreferrer">视图</a>.</p>
<p>返回一个完整的<code v-pre>Response</code> 实例允许你自定义返回的 HTTP 状态码和返回头信息。<code v-pre>Response</code>实例继承自<code v-pre>Symfony\Component\HttpFoundation\Response</code>类，该类提供了各种构建 HTTP 响应的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span></span>
<span class="line">                  <span class="token operator">-></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'text/plain'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="eloquent-模型-和-集合" tabindex="-1"><a class="header-anchor" href="#eloquent-模型-和-集合"><span>Eloquent 模型 和 集合</span></a></h4>
<p>你也可以直接从你的路由和控制器返回 <a href="https://learnku.com/docs/laravel/10.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent ORM</a> 模型和集合。当你这样做时，Laravel 将自动将模型和集合转换为 JSON 响应，同时遵循模型的 <a href="https://learnku.com/docs/laravel/10.x/eloquent-serialization#hiding-attributes-from-json" target="_blank" rel="noopener noreferrer">隐藏属性</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/user/{user}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在响应中附加-header-信息" tabindex="-1"><a class="header-anchor" href="#在响应中附加-header-信息"><span>在响应中附加 Header 信息</span></a></h3>
<p>请记住，大多数响应方法都是可以链式调用的，它允许你流畅地构建响应实例。例如，在将响应发送回用户之前，可以使用 <code v-pre>header</code> 方法将一系列头添加到响应中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token variable">$content</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token variable">$type</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'X-Header-One'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Header Value'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'X-Header-Two'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Header Value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以使用 <code v-pre>withHeaders</code> 方法指定要添加到响应的标头数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token variable">$content</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">withHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'Content-Type'</span> <span class="token operator">=></span> <span class="token variable">$type</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'X-Header-One'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Header Value'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'X-Header-Two'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Header Value'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="缓存控制中间件" tabindex="-1"><a class="header-anchor" href="#缓存控制中间件"><span>缓存控制中间件</span></a></h4>
<p>Laravel 包含一个 <code v-pre>cache.headers</code> 中间件，可用于快速设置一组路由的 <code v-pre>Cache-Control</code> 标头。指令应使用相应缓存控制指令的 蛇形命名法 等效项提供，并应以分号分隔。如果在指令列表中指定了 <code v-pre>etag</code> ，则响应内容的 MD5 哈希将自动设置为 ETag 标识符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cache.headers:public;max_age=2628000;etag'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">group</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/privacy'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/terms'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在响应中附加-cookie-信息" tabindex="-1"><a class="header-anchor" href="#在响应中附加-cookie-信息"><span>在响应中附加 Cookie 信息</span></a></h3>
<p>可以使用 <code v-pre>cookie</code> 方法将 cookie 附加到传出的 <code v-pre>illumize\Http\Response</code> 实例。你应将 cookie 的名称、值和有效分钟数传递给此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>cookie</code> 方法还接受一些使用频率较低的参数。通常，这些参数的目的和意义与 PHP 的原生 <a href="https://secure.php.net/manual/en/function.setcookie.php" target="_blank" rel="noopener noreferrer">setcookie</a> 的参数相同</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">,</span> <span class="token variable">$path</span><span class="token punctuation">,</span> <span class="token variable">$domain</span><span class="token punctuation">,</span> <span class="token variable">$secure</span><span class="token punctuation">,</span> <span class="token variable">$httpOnly</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你希望确保 cookie 与传出响应一起发送，但你还没有该响应的实例，则可以使用 <code v-pre>Cookie</code> facade 将 cookie 加入队列，以便在发送响应时附加到响应中。<code v-pre>queue</code> 方法接受创建 cookie 实例所需的参数。在发送到浏览器之前，这些 cookies 将附加到传出的响应中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Cookie</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Cookie</span><span class="token operator">::</span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="生成-cookie-实例" tabindex="-1"><a class="header-anchor" href="#生成-cookie-实例"><span>生成 Cookie 实例</span></a></h4>
<p>如果要生成一个 <code v-pre>Symfony\Component\HttpFoundation\Cookie</code> 实例，打算稍后附加到响应实例中，你可以使用全局 <code v-pre>cookie</code> 助手函数。此 cookie 将不会发送回客户端，除非它被附加到响应实例中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$cookie</span> <span class="token operator">=</span> <span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span><span class="token variable">$cookie</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="提前过期-cookies" tabindex="-1"><a class="header-anchor" href="#提前过期-cookies"><span>提前过期 Cookies</span></a></h4>
<p>你可以通过响应中的<code v-pre>withoutCookie</code>方法使 cookie 过期，用于删除 cookie ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withoutCookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果尚未有创建响应的实例，则可以使用<code v-pre>Cookie</code> facade 中的<code v-pre>expire</code> 方法使 Cookie 过期：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Cookie</span><span class="token operator">::</span><span class="token function">expire</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="cookies-和-加密" tabindex="-1"><a class="header-anchor" href="#cookies-和-加密"><span>Cookies 和 加密</span></a></h3>
<p>默认情况下，由 Laravel 生成的所有 cookie 都经过了加密和签名，因此客户端无法篡改或读取它们。如果要对应用程序生成的部分 cookie 禁用加密，可以使用<code v-pre>App\Http\Middleware\EncryptCookies</code>中间件的<code v-pre>$except</code>属性，该属性位于<code v-pre>app/Http/Middleware</code>目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 这个名字的 Cookie 将不会加密。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$except</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'cookie_name'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="重定向" tabindex="-1"><a class="header-anchor" href="#重定向"><span>重定向</span></a></h2>
<p>重定向响应是<code v-pre>Illuminate\Http\RedirectResponse</code> 类的实例，包含将用户重定向到另一个 URL 所需的适当 HTTP 头。Laravel 有几种方法可以生成<code v-pre>RedirectResponse</code>实例。最简单的方法是使用全局<code v-pre>redirect</code>助手函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'home/dashboard'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有时你可能希望将用户重定向到以前的位置，例如当提交的表单无效时。你可以使用全局 back 助手函数来执行此操作。由于此功能使用 <a href="https://learnku.com/docs/laravel/10.x/session" target="_blank" rel="noopener noreferrer">session</a>，请确保调用<code v-pre>back</code> 函数的路由使用的是<code v-pre>web</code>中间件组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/user/profile'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 验证请求参数</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="重定向到指定名称的路由" tabindex="-1"><a class="header-anchor" href="#重定向到指定名称的路由"><span>重定向到指定名称的路由</span></a></h3>
<p>当你在没有传递参数的情况下调用 <code v-pre>redirect</code> 助手函数时，将返回 <code v-pre>Illuminate\Routing\Redirector</code> 的实例，允许你调用 <code v-pre>Redirector</code> 实例上的任何方法。例如，要对命名路由生成 <code v-pre>RedirectResponse</code> ，可以使用 <code v-pre>route</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果路由中有参数，可以将其作为第二个参数传递给 <code v-pre>route</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 对于具有以下URI的路由: /profile/{id}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通过-eloquent-模型填充参数" tabindex="-1"><a class="header-anchor" href="#通过-eloquent-模型填充参数"><span>通过 Eloquent 模型填充参数</span></a></h4>
<p>如果你要重定向到使用从 Eloquent 模型填充 「ID」 参数的路由，可以直接传递模型本身。ID 将会被自动提取：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 对于具有以下URI的路由: /profile/{id}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想要自定义路由参数，你可以指定路由参数 (<code v-pre>/profile/{id:slug}</code>) 或者重写 Eloquent 模型上的 <code v-pre>getRouteKey</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取模型的路由键值。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getRouteKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">mixed</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">slug</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="重定向到控制器行为" tabindex="-1"><a class="header-anchor" href="#重定向到控制器行为"><span>重定向到控制器行为</span></a></h3>
<p>也可以生成重定向到 <a href="https://learnku.com/docs/laravel/10.x/controllers" target="_blank" rel="noopener noreferrer">controller actions</a>。只要把控制器和 action 的名称传递给 <code v-pre>action</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>UserController</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">UserController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'index'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果控制器路由有参数，可以将其作为第二个参数传递给 <code v-pre>action</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">action</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token class-name static-context">UserController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'profile'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="重定向到外部域名" tabindex="-1"><a class="header-anchor" href="#重定向到外部域名"><span>重定向到外部域名</span></a></h3>
<p>有时候你需要重定向到应用外的域名。可以通过调用<code v-pre>away</code>方法，它会创建一个不带有任何额外的 URL 编码、有效性校验和检查<code v-pre>RedirectResponse</code>实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">away</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://www.google.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="重定向并使用闪存的-session-数据" tabindex="-1"><a class="header-anchor" href="#重定向并使用闪存的-session-数据"><span>重定向并使用闪存的 Session 数据</span></a></h3>
<p>重定向到新的 URL 的同时<a href="https://learnku.com/docs/laravel/10.x/session#flash-data" target="_blank" rel="noopener noreferrer">传送数据给Session</a> 是很常见的。 通常这是在你将消息发送到 session 后成功执行操作后完成的。为了方便，你可以创建一个<code v-pre>RedirectResponse</code>实例并在链式方法调用中将数据传送给 session：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/user/profile'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'dashboard'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Profile updated!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在用户重定向后，你可以显示 <a href="https://learnku.com/docs/laravel/10.x/session" target="_blank" rel="noopener noreferrer">session</a>。例如，你可以使用 <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade 模板语法</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"alert alert-success"</span><span class="token operator">></span></span>
<span class="line">        <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">    <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line">@<span class="token keyword">endif</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="使用输入重定向" tabindex="-1"><a class="header-anchor" href="#使用输入重定向"><span>使用输入重定向</span></a></h4>
<p>你可以使用<code v-pre>RedirectResponse</code>实例提供的<code v-pre>withInput</code>方法将当前请求输入的数据发送到 session ，然后再将用户重定向到新位置。当用户遇到验证错误时，通常会执行此操作。每当输入数据被发送到 session , 你可以很简单的在下一次重新提交的表单请求中<a href="https://learnku.com/docs/laravel/10.x/requests#retrieving-old-input" target="_blank" rel="noopener noreferrer">取回它</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="其他响应类型" tabindex="-1"><a class="header-anchor" href="#其他响应类型"><span>其他响应类型</span></a></h2>
<p><code v-pre>response</code> 助手可用于生成其他类型的响应实例。当不带参数调用 <code v-pre>response</code> 助手时，会返回 <code v-pre>Illuminate\Contracts\Routing\ResponseFactory</code> <a href="https://learnku.com/docs/laravel/10.x/contracts" target="_blank" rel="noopener noreferrer">contract</a> 的实现。 该契约提供了几种有用的方法来生成响应。</p>
<h3 id="响应视图" tabindex="-1"><a class="header-anchor" href="#响应视图"><span>响应视图</span></a></h3>
<p>如果你需要控制响应的状态和标头，但还需要返回 <a href="https://learnku.com/docs/laravel/10.x/views" target="_blank" rel="noopener noreferrer">view</a> 作为响应的内容，你应该使用 <code v-pre>view</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'hello'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token variable">$type</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，如果你不需要传递自定义 HTTP 状态代码或自定义标头，则可以使用全局 <code v-pre>view</code> 辅助函数。</p>
<h3 id="json-responses" tabindex="-1"><a class="header-anchor" href="#json-responses"><span>JSON Responses</span></a></h3>
<p><code v-pre>json</code> 方法会自动将 <code v-pre>Content-Type</code> 标头设置为 <code v-pre>application/json</code>，并使用 <code v-pre>json_encode</code> PHP 函数将给定的数组转换为 JSON：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Abigail'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'CA'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想创建一个 JSONP 响应，你可以结合使用 <code v-pre>json</code> 方法和 <code v-pre>withCallback</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Abigail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'CA'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">withCallback</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'callback'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件下载" tabindex="-1"><a class="header-anchor" href="#文件下载"><span>文件下载</span></a></h3>
<p><code v-pre>download</code> 方法可用于生成强制用户浏览器在给定路径下载文件的响应。<code v-pre>download</code> 方法接受文件名作为该方法的第二个参数，这将确定下载文件的用户看到的文件名。 最后，你可以将一组 HTTP 标头作为该方法的第三个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">download</span><span class="token punctuation">(</span><span class="token variable">$pathToFile</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">download</span><span class="token punctuation">(</span><span class="token variable">$pathToFile</span><span class="token punctuation">,</span> <span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：管理文件下载的 Symfony HttpFoundation 要求正在下载的文件具有 ASCII 文件名。</p>
</blockquote>
<h4 id="流式下载" tabindex="-1"><a class="header-anchor" href="#流式下载"><span>流式下载</span></a></h4>
<p>有时你可能希望将给定操作的字符串响应转换为可下载的响应，而不必将操作的内容写入磁盘。在这种情况下，你可以使用<code v-pre>streamDownload</code>方法。此方法接受回调、文件名和可选的标头数组作为其参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Services<span class="token punctuation">\</span>GitHub</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">streamDownload</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token class-name static-context">GitHub</span><span class="token operator">::</span><span class="token function">api</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'repo'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">contents</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">readme</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'contents'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'laravel-readme.md'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件响应" tabindex="-1"><a class="header-anchor" href="#文件响应"><span>文件响应</span></a></h3>
<p><code v-pre>file</code>方法可用于直接在用户的浏览器中显示文件，例如图像或 PDF，而不是启动下载。这个方法接受文件的路径作为它的第一个参数和一个头数组作为它的第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token variable">$pathToFile</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token variable">$pathToFile</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="响应宏" tabindex="-1"><a class="header-anchor" href="#响应宏"><span>响应宏</span></a></h2>
<p>如果你想定义一个可以在各种路由和控制器中重复使用的自定义响应，你可以使用<code v-pre>Response</code> facade 上的<code v-pre>macro</code>方法。通常，你应该从应用程序的<a href="https://learnku.com/docs/laravel/10.x/providers" target="_blank" rel="noopener noreferrer">服务提供者</a>，如<code v-pre>App\Providers\AppServiceProvider</code>服务提供程序的<code v-pre>boot</code>方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Response</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 启动一个应用的服务</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Response</span><span class="token operator">::</span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'caps'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token class-name static-context">Response</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token function">strtoupper</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>macro</code>函数接受名称作为其第一个参数，并接受闭包作为其第二个参数。当从<code v-pre>ResponseFactory</code>实现或<code v-pre>response</code>助手函数调用宏名称时，将执行宏的闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">caps</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/responses/14850" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/re...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/responses/14850" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/re...</a></p>
</blockquote>
</div></template>


